package com.booth.base.controllers;

import java.sql.Timestamp;

import com.booth.base.interceptor.AuthInterceptor;
import com.booth.base.model.Groups;
import com.booth.base.model.Users;
import com.booth.base.utils.MD5;
import com.booth.base.utils.ResultStatus;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;

@Before({AuthInterceptor.class})
public class UsersController extends BaseController
{
  public void index()
  {
    render("users.jsp");
  }

  public void au_page()
  {
    int userId = getParaToInt("userId", Integer.valueOf(0)).intValue();
    if (userId > 0)
    {
      Users u = (Users)Users.dao.findById(Integer.valueOf(userId));
      Groups group = (Groups)Groups.dao.findById(u.get("group_id"));
      u.set("password", null);
      setAttr("users", u);
      setAttr("groups", group);
    }
    render("users_au.jsp");
  }
  public void list() throws Exception
  {
	  String userName = getPara("userName");
	  String loginName = getPara("loginName");
	  String groupId = getPara("groupId");
	  String sex = getPara("sex");
	  String status = getPara("status");
    Page page = Users.dao.findPage(getParaToInt("page", Integer.valueOf(1)).intValue(), getParaToInt("rows", Integer.valueOf(10)).intValue(),
    		userName,loginName,groupId,sex,status);
    setAttr("total", Integer.valueOf(page.getTotalRow()));
    setAttr("rows", page.getList());
    renderResult(ResultStatus.SUCCESS);
  }

  public void save()
  {
    boolean ret = false;
    Users users = (Users)getModel(Users.class);
    if ((users.getInt("user_id") != null) && (users.getInt("user_id").intValue() > 0))
    {
      Users old = (Users)Users.dao.findById(users.getInt("user_id"));
      users.set("login_name", old.get("login_name"));
      users.set("password", old.get("password"));
      users.set("reg_time", old.get("reg_time"));
      users.set("last_time", old.get("last_time"));
      ret = users.update();
      if (!ret)
      {
        renderResult(ResultStatus.UPDATE_FAILURE);
      }
    }
    else
    {
      String password = users.getStr("password");
      if ((password == null) || ("".equals(password.trim())))
      {
        password = "123456";
      }
      password = MD5.md5(password).substring(4, 28);
      users.set("password", password);
      users.set("reg_time", new Timestamp(System.currentTimeMillis()));
      ret = users.save();
      if (!ret)
      {
        renderResult(ResultStatus.ADD_FAILURL);
      }
    }
    if (ret)
    {
      renderResult(ResultStatus.SUCCESS);
    }
  }
  public void delete()
  {
    boolean success = false;
    int id = getParaToInt("id", Integer.valueOf(0)).intValue();
    if (id > 0)
    {
      success = new Users().deleteById(Integer.valueOf(id));
    }
    if (success)
      renderResult(ResultStatus.SUCCESS);
    else
      renderResult(ResultStatus.DELETE_FAILURE);
  }
  public void resetPassword()
  {
    int uid = getParaToInt("uid", Integer.valueOf(0)).intValue();
    if (uid > 0)
    {
      String password = MD5.md5("123456").substring(4, 28);
      boolean ret = Users.dao.changePassword(Integer.valueOf(uid), password);
      if (ret)
      {
        renderResult(ResultStatus.SUCCESS);
      }
      else
      {
        renderResult(ResultStatus.UPDATE_PASSWORD_FAILURE);
      }
    }
    else
    {
      renderResult(ResultStatus.NO_NEED_DATA);
    }
  }
  public void checkUniqueLoginName(){
	  String loginName = getPara("loginName");
	  Long count = Users.dao.checkUniqueLoginName(loginName);
	  if(count == null || count <= 0){
		  renderResult(ResultStatus.SUCCESS);
	  } else {
		  renderResult(ResultStatus.EXCEPTION);
	  }
  }
}